package com.atguigu.yygh.hosp.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.hosp.repository.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;

import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    // 医院详情信息
    public Map<String, Object> getHospById(String id){
        Map<String, Object> result = new HashMap<>();
        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());

        result.put("hospital", hospital);
        // 将医院里面中的属性单独提取出来，更加直观
        result.put("bookingRule", hospital.getBookingRule());
        // 不需要重复显示
        hospital.setBookingRule(null);
        return result;
    }

    // 更新医院上线状态
    @Override
    public void updateStatus(String id, Integer status){
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo){
        // 创建Pageable对象，设置当前页和每页记录数
        Pageable pageable = PageRequest.of(page-1, limit);
        // 创建Example对象
        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);

        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);

        Example<Hospital> example = Example.of(hospital,matcher);
        // 查询出所有的数据
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);
        
        // 遍历医院数据获取另外一个模块的数据
        // getContent()获取对象中的list集合
        List<Hospital> list = pages.getContent();
        // 使用流的方式遍历
        list.stream().forEach(item->{
            this.setHospitalHosType(item);
        });

        return pages;
    }

    private Hospital setHospitalHosType(Hospital hospital){
        // 根据dictCode和Value获取医院等级名称
        String dictName = dictFeignClient.getName("Hostype", hospital.getHostype());
        // 查询省 市 区
        String provinceName = dictFeignClient.getName(hospital.getProvinceCode());
        String cityName = dictFeignClient.getName(hospital.getCityCode());
        String districtName = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("hostypeString", dictName);
        hospital.getParam().put("fullAddress", provinceName+cityName+districtName);
        return hospital;
    }


    //获取医院名称
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(hospital != null) {
            return hospital.getHosname();
        }
        return null;
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    @Override
    public void save(Map<String, Object> paramMap) {
        //把参数map集合转换对象 Hospital
        String mapString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);

        //判断是否存在数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);

        //如果存在，进行修改
        if(hospitalExist != null) {
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else {//如果不存在，进行添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    // 根据医院编号获取医院预约挂号详情
    @Override
    public Map<String, Object> item(String hoscode){
        Map<String, Object> result = new HashMap<>();
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));

        result.put("hospital", hospital);
        // 将医院里面中的属性单独提取出来，更加直观
        result.put("bookingRule", hospital.getBookingRule());
        // 不需要重复显示
        hospital.setBookingRule(null);
        return result;
    }
}
